<template>
  <div style="width: 30%; margin: 100px auto">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        :label='$t("User.email")'
        prop="Email"
        :rules="[
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change'],
          },
        ]"
      >
        <el-input v-model="ruleForm.Email"></el-input>
      </el-form-item>
      <el-form-item :label='$t("User.psd")' prop="Psd">
        <el-input
          type="password"
          v-model="ruleForm.Psd"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >{{$t("User.login")}}</el-button
        >
        <el-button @click="resetForm('ruleForm')">{{$t("User.reset")}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { mapActions } from "vuex";

export default {
  data() {
    return {
      ruleForm: {
        Email: "",
        Psd: "",
      },
      rules: {
        Psd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapGetters(["user"]),
  },
  methods: {
    ...mapActions({
      setUserEmail:"user/setUserEmail",
    }),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$api.user.login(this.ruleForm).then((data) => {
            console.log(data)
            if (data.code == 0) {
              this.setUserEmail(data.data[0].Email)
              this.$message.success("登录成功");
              window.sessionStorage.setItem("userEmail", data.data);
              this.$router.push({ name: "Home" });
              return;
            }else {
              this.ruleForm.Psd = "";
              this.$message.error("登录失败！！！");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  created() {
    console.log(this.$api.user);
    console.log(this.$store);
  },
};
</script>
<style>
</style>